package javaExer.草稿;

import java.util.*;

/**
 * @author 徐州城
 * @version 1.0
 */
public class Map遍历{

    public static void main(String[] args) {

        Map<Object,Object> map = new HashMap();
        map.put("1","张三");
        map.put("2","李四");
        map.put("3","王五");
        map.put("null","田六");

        //第一组：先取出所有的Key，通过Key 取出对应的Value
        System.out.println("===通过KeySet()遍历数组===");
        Set<Object> objects = map.keySet();
        for (Object key : objects) {
            System.out.println(key + "-" + map.get(key));
        }

        Iterator<Object> iterator = map.keySet().iterator();
        while (iterator.hasNext()) {
            Object key = iterator.next();
            System.out.println(key + "-" + map.get(key));
        }


        //第二组: 把所有的values取出
        System.out.println("===通过Values()方式遍历值===");
        Collection<Object> values = map.values();
        for (Object obj : values) {
            System.out.println(obj);
        }

        Iterator<Object> iterator1 = map.values().iterator();
        while (iterator1.hasNext()) {
            Object val = iterator1.next();
            System.out.println(val);
        }

        //第三组：通过EntrySet 来获取 k-v
        System.out.println("===通过entrySet()方式遍历===");
        Set<Map.Entry<Object, Object>> entries = map.entrySet();
        for (Object obj : entries) {
            System.out.println(obj);
        }

        Iterator<Map.Entry<Object, Object>> iterator2 = entries.iterator();
        while (iterator2.hasNext()) {
            Object next = iterator2.next();
            System.out.println(((Map.Entry)next).getKey() + "-" + ((Map.Entry)next).getValue());
        }
    }
}

